package Day8.ZuoYe;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;

//7:获得文件行数的方法有 bufferedReader.readLine 方法 或者查找byte是否=='\n',比较谁快！
public class Zuoye7 {
    public static void main(String[] args) {
        try {
            long a1 = System.nanoTime();
            BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\java课程及作业\\wang\\4567.txt")));
            String s = null;
            int count = 0;
            while ((s = bf.readLine()) != null) {
                count++;
                //            System.out.println(s);
            }
            bf.close();
            System.out.println("文件行数：" + count + "行。");
            long a2 = System.nanoTime();
            long b1 = System.nanoTime();
            bf = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\java课程及作业\\wang\\4567.txt")));
            int i = 0, count1 = 0;
            while ((i = bf.read()) != -1) {
                if ((char) i == '\n') {
                    count1++;
                }
            }
            bf.close();
            System.out.println("文件行数：" + count + "行。");
            long b2 = System.nanoTime();
            long a = a2 - a1;
            long b = b2 - b1;
            System.out.println("readLine 方法" + a + "纳秒");
            System.out.println("byte是否=='\\n'方法" + b + "纳秒");
            if (a > b) System.out.println("byte是否=='\\n'快");
            else if (a == b) System.out.println("一样快");
            else System.out.println("readLine 方法快");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
